﻿using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Windows.Devices.Geolocation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Maps.Toolkit;
using Microsoft.Phone.Shell;
using SMARTMobile.Common;
using SmartMobile.Data;

namespace SMARTMobile.Views
{
    public partial class BanDoPage : PhoneApplicationPage
    {
        private GeoCoordinate MyCoordinate = null;

        private double _accuracy = 0.0;
        private MapLayer myLocationLayer;
        private MapLayer tapLocationLayer;
        private GeoCoordinate tapLocation;
        private bool _isChonViTri;
        private ProgressIndicator _progressIndicator;
        private TCARCUST currentCus;
        private SmartMobileDBContext smartMobileContext;

        public BanDoPage()
        {
            InitializeComponent();
            //this.Loaded += (s, e) => SystemTray.ProgressIndicator = new ProgressIndicator();
            _progressIndicator = new ProgressIndicator();
            _progressIndicator.IsVisible = true;
            _progressIndicator.IsIndeterminate = true;
            _progressIndicator.Text = "My status";
            SystemTray.SetProgressIndicator(this, _progressIndicator);
            this.SetProgressIndicator(true);
            myLocationLayer = new MapLayer();
            tapLocationLayer = new MapLayer();
            this.ShowMyLocationOnTheMap();
        }

        private void ChonViTriButton_Click(object sender, EventArgs e)
        {
            this._isChonViTri = true;
        }


        private void appBarButton1_Click(object sender, EventArgs e)
        {
            double distanceToMSFT = tapLocation.GetDistanceTo(MyCoordinate);
            MessageBox.Show("It is " + distanceToMSFT.ToString() + " meters from there to Microsoft!");
        }

        private async void ShowMyLocationOnTheMap()
        {

            this.smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);

            // It will copy from local to iso
            smartMobileContext.CreateIfNotExists();
            ////if (GlobalInformation.DSKhachHang == null)
            ////{
            ////    GlobalInformation.DSKhachHang = smartMobileContext.ARCUST.ToList();
            ////}

            int index = 1;
            foreach (var cus in smartMobileContext.Shopvisit.ToList())
            {
                if (cus.Lat != "0" && cus.Long != "0")
                {
                    // Create a small circle to mark the current location.
                    ////Ellipse myCircle = new Ellipse();
                    ////myCircle.Fill = new SolidColorBrush(Colors.Green);
                    ////myCircle.Height = 20;
                    ////myCircle.Width = 20;
                    ////myCircle.Opacity = 50;
                    ////myCircle.Tap += MyCircle_Tap;
                    ////myCircle.Tag = cus;

                    ////// Create a MapOverlay to contain the circle.
                    ////MapOverlay myLocationOverlay = new MapOverlay();
                    ////myLocationOverlay.Content = myCircle;
                    ////myLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
                    ////myLocationOverlay.GeoCoordinate = new GeoCoordinate(double.Parse(cus.Lat), double.Parse(cus.Long));

                    // Create a MapLayer to contain the MapOverlay.


                    Pushpin pushpin = new Pushpin();
                    pushpin.GeoCoordinate = new GeoCoordinate(double.Parse(cus.Lat), double.Parse(cus.Long));
                    pushpin.Background = new SolidColorBrush(Colors.Red);
                    pushpin.Tap += MyCircle_Tap;
                    pushpin.Tag = cus;
                    ////pushpin.Content = index++ + ". " + cus.Customername;
                    pushpin.Content = index++;

                    MapOverlay myLocationOverlay = new MapOverlay();
                    myLocationOverlay.Content = pushpin;
                    myLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
                    myLocationOverlay.GeoCoordinate = new GeoCoordinate(double.Parse(cus.Lat), double.Parse(cus.Long));
                    myLocationLayer.Add(myLocationOverlay);
                }
            }

            // Add the MapLayer to the Map.
            MyMap.Layers.Add(myLocationLayer);
            this.SetProgressIndicator(false);
        }

        private void MyCircle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            my_popup_xaml.Visibility = Visibility.Visible;
            var currentShopvisit = (sender as Pushpin).Tag as Shopvisit;
            currentCus = this.smartMobileContext.TCARCUST.FirstOrDefault(x => x.Customerkey == currentShopvisit.CustomerKey);
            ThongTinArCusTextBlock.Text = currentCus.Customername + " - " + currentCus.Address + ", " + currentCus.Street + ", " + currentCus.Ward +
                                          ", " + currentCus.Provice;
        }

        private void MyMap_OnTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (this._isChonViTri)
            {
                this._isChonViTri = false;


                tapLocation =
                    MyMap.ConvertViewportPointToGeoCoordinate(e.GetPosition((UIElement)sender));
                // Create a small circle to mark the current location.
                Ellipse myCircle = new Ellipse();
                myCircle.Fill = new SolidColorBrush(Colors.Green);
                myCircle.Height = 20;
                myCircle.Width = 20;
                myCircle.Opacity = 50;

                // Create a MapOverlay to contain the circle.
                MapOverlay myLocationOverlay = new MapOverlay();
                myLocationOverlay.Content = myCircle;
                myLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
                myLocationOverlay.GeoCoordinate = tapLocation;

                tapLocationLayer.Clear();

                // Create a MapLayer to contain the MapOverlay.
                tapLocationLayer.Add(myLocationOverlay);

                // Add the MapLayer to the Map.
                if (!MyMap.Layers.Contains(tapLocationLayer))
                {
                    MyMap.Layers.Add(tapLocationLayer);
                }
            }
        }

        private void SetProgressIndicator(bool value)
        {
            _progressIndicator.IsIndeterminate = value;
            _progressIndicator.IsVisible = value;
        }

        private void LayViTri_Click(object sender, EventArgs e)
        {
            this.ShowMyLocationOnTheMap();
        }

        private void GanViTri_Click(object sender, EventArgs e)
        {
            if (this.MyCoordinate != null)
            {
                NavigationService.Navigate(new Uri("/Views/DSKhachHangPage.xaml", UriKind.Relative));
            }
        }

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));
        }

        private void ZoomIn_Clicked(object sender, RoutedEventArgs e)
        {
            if (MyMap.ZoomLevel < 20)
            {
                MyMap.ZoomLevel++;
            }
        }
        private void ZoomOut_Clicked(object sender, RoutedEventArgs e)
        {
            if (MyMap.ZoomLevel > 1)
            {
                MyMap.ZoomLevel--;
            }
        }

        private void ThoatButton_OnClick(object sender, RoutedEventArgs e)
        {
            my_popup_xaml.Visibility = Visibility.Collapsed;
        }

        private void CapNhatButton_OnClick(object sender, RoutedEventArgs e)
        {
            GlobalInformation.SelectedTCARCUS = this.currentCus;
            NavigationService.Navigate(new Uri("/Views/CapNhatKhachHangPage.xaml", UriKind.Relative));
        }
    }
}